package cn.xspace.module.other.activity;

import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;

import com.xspace.android.imcommon.interfaces.NotifyMg;
import com.xspace.android.xafcommon.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import cn.xspace.app.view.TitleBar;
import cn.xspace.module.other.R;

public class AlarmActivity extends BaseActivity {

    private TitleBar mTitleBar;
    private ListView mListView;
    private ReminderAdapter mAdapter;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_alarm);
    }

    @Override
    protected void initFindViewById() {
        mTitleBar = (TitleBar) findViewById(R.id.alarm_title_bar);
        mListView = (ListView) findViewById(R.id.lv_alarm);
    }

    @Override
    protected void initData() {
        mAdapter = new ReminderAdapter(getSettings());
        mListView.setAdapter(mAdapter);
    }

    @Override
    protected void initEvent() {
        mTitleBar.setLeftLayoutClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    public class AlarmInfo {
        String title;
        boolean isSelected;
        int index;

        public AlarmInfo(String title, boolean isSelected, int index) {
            this.title = title;
            this.isSelected = isSelected;
            this.index = index;
        }
    }

    enum AlarmType {
        STATUS_BAR_REMINDER, VOICE_REMINDER, VIBRATION_REMINDER
    }

    private List<AlarmInfo> getSettings() {
        boolean status = NotifyMg.getInstance().isNotifyOpen();
        boolean voice = NotifyMg.getInstance().isNotifyAlert();
        boolean vibration = NotifyMg.getInstance().isNotifyvibation();

        List<AlarmInfo> list = new ArrayList<>();
        list.add(new AlarmInfo("状态栏提醒", status, AlarmType.STATUS_BAR_REMINDER.ordinal()));
        list.add(new AlarmInfo("声音提醒", voice, AlarmType.VOICE_REMINDER.ordinal()));
        list.add(new AlarmInfo("震动提醒", vibration, AlarmType.VIBRATION_REMINDER.ordinal()));
        return list;
    }

    public class ReminderAdapter extends BaseAdapter {

        List<AlarmInfo> mList = null;

        public ReminderAdapter(List<AlarmInfo> settings) {
            mList = settings;
        }

        @Override
        public int getCount() {
            return mList.size();
        }

        @Override
        public Object getItem(int arg0) {
            return arg0;
        }

        @Override
        public long getItemId(int arg0) {
            return arg0;
        }

        @Override
        public View getView(int position, View view, ViewGroup parent) {
            final AlarmInfo value = mList.get(position);
            View convertView = View.inflate(mContext, R.layout.alarm_list_item, null);
            TextView title = (TextView) convertView.findViewById(R.id.tv_alarm_type);
            CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.alarm_item_checkbox);
            title.setText(value.title);
            if (value.isSelected) {
                checkBox.setChecked(true);
            } else {
                checkBox.setChecked(false);
            }
//            if (0 == position) {
//                convertView.setBackgroundResource(R.drawable.preference_first_item);
//            } else if (mAdapter.getCount() - 1 == position) {
//                convertView.setBackgroundResource(R.drawable.preference_last_item);
//            } else {
//                convertView.setBackgroundResource(R.drawable.preference_item);
//            }
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    value.isSelected = !value.isSelected;
                    if (value.index == AlarmType.STATUS_BAR_REMINDER.ordinal()) {
                        NotifyMg.getInstance().setNotifyOpen(value.isSelected);
                    } else if (value.index == AlarmType.VOICE_REMINDER.ordinal()) {
                        NotifyMg.getInstance().setNotifyAlert(value.isSelected);
                    } else if (value.index == AlarmType.VIBRATION_REMINDER.ordinal()) {
                        NotifyMg.getInstance().setNotifyvibation(value.isSelected);
                    }
                    mAdapter.notifyDataSetChanged();
                }
            });
            return convertView;
        }
    }

}
